<?php
/**
 * 管理组模型
 * @author hedong <2015-12-07 16:31:45>
 */
namespace OrderAdmin\Model;

use Carbon\Carbon;
use Think\Model\RelationModel;

class WorkerModel extends RelationModel {

    /**
     * 获取总数
     * @return mixed
     */
    function getAllCount(){
        return $this->count();
    }

    /**
     * 获取当月总数
     * @return mixed
     */
    function getCountThisMonth(){
        $startTime = Carbon::now()->firstOfMonth()->toDateTimeString();
        $endTime = Carbon::now()->firstOfMonth()->addMonth(1)->toDateTimeString();
        return $this->where(['add_time'=>['between', [$startTime, $endTime]]])->count();
    }

    /**
     * 分页查询
     * @param $param
     * @return array
     */
    function getData($param){
        // 请求参数
        if($param['key']){
            $where['a.mobile'] = ['like', '%'.$param['key'].'%'];
            $where['a.name'] = ['like', '%'.$param['key'].'%'];
        }
        if($param['province']){
            $where2['a.province'] = $param['province'];
        }
        if($param['city']) {
            $where2['a.city'] = $param['city'];
        }
        if($param['status']){
            $where2['a.status'] = ['in', $param['status']];
        }
        if($param['partner_id']){
            $where2['a.partner_id'] = $param['partner_id'];
        }
        if($where && $where2){
            $where['_logic'] = 'or';
            $where2['_complex'] = $where;
        }elseif($where && !$where2){
            $where['_logic'] = 'or';
            $where2 = $where;
        }elseif(!$where && !$where2){
            $where2 = [];
        }

        // 获取数据
        $pageNum = ($param['page'] - 1) * $param['size'];
        $data['count'] = $this->alias('a')->join('LEFT JOIN __PARTNER__ b ON a.partner_id = b.id')->where($where2)->count();
        if($data['count'] < $pageNum){
            return ['status' => false, 'msg'=>'没有更多数据了'];
        }
        $data['data'] = $this->alias('a')
            ->field('a.id, a.name, a.mobile, a.status, a.audit_info, a.audit_time, concat_ws("/", a.province, a.city) province_city, 
            a.status, b.name partner_name, c.filepath headimg')
            ->join('LEFT JOIN __PARTNER__ b ON a.partner_id = b.id')
            ->join('LEFT JOIN __FILES__ c ON a.headimg = c.file_id')
            ->where($where2)
            ->order('a.add_time desc')
            ->limit($pageNum, $param['size'])
            ->select();

        foreach ($data['data'] as &$v){
            $v['headimg'] = makeImagePath($v['headimg']);
        }
        unset($v);

        return ['status'=> true, 'data'=> $data];
    }

    /**
     * 获取师傅信息
     * @param $id
     */
    public function getInfoById($id){
        $data = $this->alias('a')
            ->field('a.id, a.name, a.mobile, a.city, a.certid, a.service_area, a.add_time, a.sex, a.age, a.specialty,
            b.name partner_name')
            ->join('LEFT JOIN __PARTNER__ b ON a.partner_id = b.id')
            ->where(['a.id'=>$id])->find();
        return $data ? : false;
    }
}
